//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Microsoft.TridentWordAddIn.Common
{
    public class WorkflowJob : DomainBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowJob"/> class.
        /// </summary>
        public WorkflowJob()
        {
            this.Parameters = new List<JobParameter>();
            this.Outputs = new List<ShallowJobOutput>();
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id { get; set; }

        /// <summary>
        /// Gets the display id.
        /// </summary>
        /// <value>The display id.</value>
        public string DisplayId
        {
            get
            {
                return this.Id.ToString();
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the name of the workflow.
        /// </summary>
        /// <value>The name of the workflow.</value>
        public Guid WorkflowId { get; set; }

        /// <summary>
        /// Gets or sets the date submitted.
        /// </summary>
        /// <value>The date submitted.</value>
        public DateTime DateSubmitted { get; set; }

        /// <summary>
        /// Gets or sets the date completed.
        /// </summary>
        /// <value>The date completed.</value>
        public DateTime DateCompleted { get; set; }

        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        /// <value>The status.</value>
        public WorkflowJobStatus Status { get; set; }

        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        /// <value>The parameters.</value>
        public List<JobParameter> Parameters { get; set; }

        /// <summary>
        /// Gets or sets the outputs.
        /// </summary>
        /// <value>The outputs.</value>
        public List<ShallowJobOutput> Outputs { get; set; }

        /// <summary>
        /// Gets or sets the schedule by.
        /// </summary>
        /// <value>The schedule by.</value>
        public string ScheduledBy { get; set; }

        /// <summary>
        /// Gets the parameter.
        /// </summary>
        /// <param name="activityName">Name of the activity.</param>
        /// <param name="paramName">Name of the param.</param>
        /// <returns></returns>
        public JobParameter GetParameter(string activityName, string paramName)
        {
            foreach (JobParameter param in this.Parameters)
            {
                if (param.ActivityName.CompareIgnoreCase(activityName) && param.Name.CompareIgnoreCase(paramName))
                {
                    return param;
                }
            }

            return null;
        }

        /// <summary>
        /// Gets the name of the workflow output file.
        /// </summary>
        /// <param name="jobId">The job id.</param>
        /// <returns></returns>
        public string GetWorkflowOutputFileName(Guid jobId)
        {
            string fileName = string.Empty;
            if (this.Outputs != null)
            {
                foreach (ShallowJobOutput output in this.Outputs)
                {
                    if (output.Id == jobId)
                    {
                        fileName = output.Name + output.Extension;
                        break;
                    }
                }
            }

            return fileName;
        }
    }

    public class WorkflowJobComparer : IComparer<WorkflowJob>
    {
        int sortOrderModifier = 1;

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowJobComparer"/> class.
        /// </summary>
        /// <param name="sortOrder">The sort order.</param>
        public WorkflowJobComparer(SortOrder sortOrder)
        {
            if (sortOrder == SortOrder.Descending)
            {
                sortOrderModifier = -1;
            }
            else if (sortOrder == SortOrder.Ascending)
            {
                sortOrderModifier = 1;
            }
        }

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// Value
        /// Condition
        /// Less than zero
        /// <paramref name="x"/> is less than <paramref name="y"/>.
        /// Zero
        /// <paramref name="x"/> equals <paramref name="y"/>.
        /// Greater than zero
        /// <paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        public int Compare(WorkflowJob x, WorkflowJob y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (y == null)
                {
                    return 1;
                }
                else
                {
                    int retval = x.DateSubmitted.CompareTo(y.DateSubmitted);
                    return retval * sortOrderModifier;
                }
            }
        }
    }
}
